package com.mbc.service.impl;


import com.fasterxml.jackson.annotation.JsonFormat;
import com.mbc.mapper.LoveContributionsMapper;
import com.mbc.pojo.MbcLoveContributions;
import com.mbc.service.LoveContributionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 * 爱心贡献表 服务实现类
 * </p>
 *
 * @author suimeng
 * @since 2024-04-08
 */
@Service
public class LoveContributionsServiceImpl implements LoveContributionsService {

    @Autowired
    private LoveContributionsMapper loveContributionsMapper;


    /**
     * 按用户 ID 获取
     *
     * @param userId 用户 ID
     * @return {@link MbcLoveContributions}
     */
    @Override
    public MbcLoveContributions getByUserId(String userId) {
        MbcLoveContributions loveContributions = loveContributionsMapper.getByUserId(userId);
        return loveContributions;
    }

    /**
     * 更新爱心贡献
     *
     * @param userId 用户 ID
     */
    @Override
    public Boolean updateLoveContribution(String userId) {
        //生成调用接口当前时间
        LocalDateTime now = LocalDateTime.now();
        //不能用格式化的时间来比较
//        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //获取月份
        int month= now.getMonthValue();
        return loveContributionsMapper.updateLoveContribution(now,month,userId) > 0;
    }
}
